var app = getApp()
var fun = require('../fun.js');

Page({

  data: {
    nav_top:app.globalData.nav_top,nav_title:'用户中心',show_cart:app.globalData.show_cart,can_load:1,page:1
  },


  onLoad: function (op) {
var user_id=app.globalData.user_id;
if(!user_id || user_id==0){this.setData({login_show:'show'});}
var that=this;


  },
  login: function (e) {
    var c = e.currentTarget.dataset.c;
    if(c=='close'){this.setData({login_show:'hide'});return;}
    fun.go_login().then(res => {
      if (res == 'ok') {
        app.msg('登录成功');
        this.load();
        this.setData({ login_show: 'hide' });
      }
    });

  },

  onReady: function () {
    
  },


  onShow: function () {
    this.load();
  },


  load: function (val) {
var id=this.data.id;
var that=this;
var page=this.data.page;if(!page){page=1;}
if(!val){val='';}
wx.request({
  url: app.globalData.server,
  data:{
ac:'get_group',user_id:app.globalData.user_id,session:app.globalData.session,id:id,page:page,val:val

  },success(res){
if(!res.data){return;}console.log(res.data)
if(res.data.err!='ok'){
if(res.data.err=='no_login'){that.setData({login_show:'show'});return}
app.err(res.data.err);return;

}

if(res.data.list){
if(page==1){
  that.setData({list:res.data.list,can_load:1})
}else{
var list=that.data.list;
if(!list){list=[]}
for(var i in res.data.list){
list.push(res.data.list[i]);

}
that.setData({list:list,can_load:1})

}
}else{that.setData({can_load:0})}

that.setData({bg:res.data.bg,user:res.data.user})
  }
})


  },
  go:function(e){
  var url=e.currentTarget.dataset.url;
  wx.navigateTo({
    url: url,
  })
  },
back:function(){
wx.navigateBack({
  delta: 0,
})

},

  
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },get_power(e){
    var jl=this.data.jl;
    var shop=this.data.user;
    if(shop.power!='adm'){return;}
    var u=e.currentTarget.dataset.id;
    var that=this;
  wx.request({
    url: app.globalData.server,
    data:{
      ac:'get_power',user_id:app.globalData.user_id,session:app.globalData.session,u:u
    },success(res){
      console.log(res.data)
  if(res.data.err!='ok'){app.err(res.data.err);return}
  app.globalData.user_id=res.data.user.id;
  app.globalData.session=res.data.user.session;
  app.globalData.user_info = res.data.user;
  try {
    wx.setStorageSync('login_info', JSON.stringify(res.data.user))
  } catch (e) { }
  that.load();
  
    }
  })
  
   },call(e){
     wx.makePhoneCall({
       phoneNumber: e.currentTarget.dataset.phone,
     })
   },onPageScroll(e){
    console.log(e);
    var that=this;
    var height=app.globalData.win_height
    var query = wx.createSelectorQuery();
    var can_load=this.data.can_load;if(!can_load){can_load=0;}
    var page=that.data.page;if(page<1){return;}if(!page){page=1;}
    var c=this.data.c;
    query.select('.main').boundingClientRect(function (res) {
      console.log(res.height);
    var x=res.height-e.scrollTop-height;
    console.log(x);
    
    if(x<100){
    if(can_load==0){return;}
    page++;
    that.setData({can_load:0,page:page})
    that.load(c)
    
    }
    }).exec()
    
    
      },search(e){
var val=e.detail.value;
if(val){
this.load(val);
}

      },tuanzhang(e){

this.load('tuanzhang');

      },pub(e){
        this.load('pub');
      }
})